using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GameXNA_Master
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class SoundHandler : Microsoft.Xna.Framework.GameComponent
    {
        SoundEffect maintheme;
        SoundEffect laser;
        SoundEffectInstance maintheme_instance;
        SoundEffectInstance laser_instance;
        Song gamesound;
        SettingsData settingsdata;
        public SoundHandler(Game game)
            : base(game)
        {
            
        }

        
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            settingsdata = (SettingsData)Game.Services.GetService(typeof(SettingsData));
            maintheme = Game.Content.Load<SoundEffect>(@"Sound/The_Walking_Dead_Theme");
            gamesound = Game.Content.Load<Song>(@"Sound/Deep Space - Origen");
            laser = Game.Content.Load<SoundEffect>(@"Sound/laserfire3");

            maintheme_instance = maintheme.CreateInstance();
            laser_instance = laser.CreateInstance();

            maintheme_instance.IsLooped = true;
            MediaPlayer.Play(gamesound);
            MediaPlayer.Pause();
            
            MediaPlayer.IsRepeating = true;


            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            MediaPlayer.Volume = settingsdata.musicVolume;
            maintheme_instance.Volume = settingsdata.musicVolume;

            if (gameInfoState.pause)
            {
                if (maintheme_instance.State == SoundState.Stopped)
                    maintheme_instance.Play();
                if (MediaPlayer.State == MediaState.Playing)
                    MediaPlayer.Pause();

            }
            else
            {
                maintheme_instance.Stop();
                MediaPlayer.Resume();
                /*switch (MediaPlayer.State)
                {
                    case MediaState.Stopped: MediaPlayer.Play(gamesound); break;
                    case MediaState.Paused: MediaPlayer.Resume(); break;
                    default: break;
                }*/

            }




            if (!gameInfoState.pause && BulletLogic.playsound && !gameInfoState.Start_new_Level )
            {
               
                laser_instance.Volume = settingsdata.effectVolume;
                laser_instance.Play();

            }
            else
            {
                if(BulletLogic.shooting)
                    laser_instance.Stop();
                //if (!gameInfoState.pause)
                  //  laser_instance.Volume = (laser_instance.Volume > 0.2f) ? laser_instance.Volume - (float)gameTime.ElapsedGameTime.TotalSeconds * 2 : 0;

            }
            
            
            base.Update(gameTime);
        }
    }
}
